﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;

using System.Web.UI.HtmlControls;

namespace GMS
{
    public partial class Pages_SelectEdge : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                rptrEdges.DataSource = Common.GetEdges();
                rptrEdges.DataBind();
            }
        }

        protected void rptrEdges_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) && 
                string.Compare(e.CommandName.ToLower(), "click") == 0)
            {
                SessionVariable.Instance.UserDetails.Options.EdgeId = Convert.ToInt32(e.CommandArgument);
                SessionVariable.Instance.UserDetails.Cost.EdgePrice = Common.GetEdgeById(SessionVariable.Instance.UserDetails.Options.EdgeId).Price;

                  using (Repeater rptr = (Repeater)e.Item.Parent)
                {
                    if (rptr != null)
                    {
                        foreach (RepeaterItem item in rptr.Items)
                        {
                            using (HtmlGenericControl dvImg = (HtmlGenericControl)item.FindControl("dvImg"))
                            {
                                if (dvImg != null)
                                {
                                    dvImg.Style.Add(HtmlTextWriterStyle.BorderColor, "transparent");
                                }
                            }
                        }
                    }
                }

                using (HtmlGenericControl dvImg = (HtmlGenericControl)e.Item.FindControl("dvImg"))
                {
                    if (dvImg != null)
                    {
                        dvImg.Style.Add(HtmlTextWriterStyle.BorderColor, "Green");
                        dvImg.Style.Add(HtmlTextWriterStyle.BorderWidth, "4px");
                        nextPage();
                    }
                }
            }
        }

        protected void rptrSelectEdge_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ImageButton imgBtn1 = (ImageButton)e.Item.FindControl("imgBtn1");

                using (HtmlGenericControl dvImg = (HtmlGenericControl)e.Item.FindControl("dvImg"))
                {
                    if (dvImg != null)
                    {
                        dvImg.Attributes["onmouseover"] = "this.style.borderColor='Red'";
                        dvImg.Attributes["onmouseout"] = "this.style.borderColor='transparent'";
                    }
                }

                if (!string.IsNullOrEmpty(imgBtn1.ToolTip))
                {
                    if (Convert.ToInt32(imgBtn1.ToolTip) == SessionVariable.Instance.UserDetails.Options.EdgeId)
                    {
                        using (HtmlGenericControl dvImg = (HtmlGenericControl)e.Item.FindControl("dvImg"))
                        {
                            if (dvImg != null)
                            {
                                dvImg.Style.Add(HtmlTextWriterStyle.BorderColor, "Green");
                                dvImg.Style.Add(HtmlTextWriterStyle.BorderWidth, "1px");
                            }
                        }
                    }
                }
            }
        }



        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("StoneSelectionInfo.aspx");
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            nextPage();
        }
        private void nextPage()
        {
            if (SessionVariable.Instance.UserDetails.IsDataPersisted)
            {
                Response.Redirect("Estimate.aspx");
            }
            else
            {
                if (SessionVariable.Instance.UserDetails.Options.EdgeId > 0 &&
                    SessionVariable.Instance.UserDetails.Cost.Edge > 0 &&
                    SessionVariable.Instance.UserDetails.Cost.EdgePrice >= 0)
                {
                    Response.Redirect("SinkOptions.aspx");
                }
            }
        }
    }
}